#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"


Date::Date(int year , int month , int day )
{
	_year = year;
	_month = month;
	_day = day;
}

Date::Date(const Date& dd)
{
	_year = dd._year;
	_month = dd._month;
	_day = dd._day;
}

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

int Date::GetMonthDay(int year, int month)
{
	assert(year >= 1 && month >= 1 && month <= 12);
	int arrayMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return arrayMonth[month];
}

Date& Date:: operator += (int x)
{
	_day += x;
	while (_day > GetMonthDay(_year, _month))
	{

		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}
//
Date Date:: operator + (int x)
{
	Date tmp(*this);
	tmp += x;
	return tmp;
}
//
//
bool Date::operator == (const Date& dd)
{
	return _year == dd._year
		&& _month == dd._month
		&& _day == dd._day;
}
//
bool Date:: operator != (const Date& dd)
{
	return !(_day == dd._day
		&& _month == dd._month
		&& _year == dd._year);
}
//
bool Date::operator>(const Date& y)
{
	if (_year > y._year)
	{
		return true;
	}
	else if (_year == y._year && _month > y._month)
	{
		return true;
	}
	else if (_year == y._year && _month == y._month && _day > y._day)
	{
		return true;
	}
	return false;
}

//
bool Date::operator >= (const Date& dd)
{
	return (*this > dd) || (*this == dd);
}
//
//bool Date::operator < (const Date& dd)
//{
//	return !(*this >= dd);
//}
//
//bool Date::operator <= (const Date& dd)
//{
//	return !(*this > dd);
//}


Date& Date::operator-=(int x)
{
	if (x < 0)
	{
		return *this += (-x);
	}
	_day -= x;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	
	return *this;
}


Date Date::operator-(int x)
{
	Date tmp(*this);
	tmp -= x;
	return tmp;
}



//++d1
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

// d1++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

bool Date::operator<(const Date& y)
{
	return !(*this >= y);
}

bool Date::operator<=(const Date& y)
{
	return !(*this > y);
}

int Date::operator - (const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;

	if (  *this < d)
	{
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		min++;
		n++;
	}

	return n;
}

ostream& operator << (ostream& out, const Date& d)
{
	out << d._year <<" " << d._month <<" " << d._day << " " << endl;
	return out;
}

istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
//ostream& operator<<(ostream& out, const Date& d)
//{
//	out << d._year << "" << d._month << "" << d._day << "" << endl;
//	return out;
//}
//
//istream& operator>>(istream& in, Date& d)
//{
//	in >> d._year >> d._month >> d._day;
//	return in;
//}